package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum ExecutionType implements EnumTrans<CompleteEntity.ExecutionType, ExecutionType> {
    /**
     * DEFAULT_EXECUTION_TYPE
     * SEND 发送给
     * SOLIDIFY 固化
     */
    DEFAULT_EXECUTION_TYPE(CompleteEntity.ExecutionType.DEFAULT_EXECUTION_TYPE),
    SEND(CompleteEntity.ExecutionType.SEND),
    SOLIDIFY(CompleteEntity.ExecutionType.SOLIDIFY),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.ExecutionType executionType;
    private static final long serialVersionUID = 987654321987654321L;

    ExecutionType(CompleteEntity.ExecutionType executionType) {
        this.id = executionType.getNumber();
        this.name = executionType.name();
        this.executionType = executionType;
    }

    /**
     * @param executionType
     * @return 对应的类型
     */
    public static ExecutionType getExecutionType(CompleteEntity.ExecutionType executionType) {
        Objects.requireNonNull(executionType, "executionType can not be null.");
        return Arrays.stream(ExecutionType.values())
                .filter(e -> e.name.equals(executionType.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.ExecutionType toProto() {
        return this.executionType;
    }

    @Override
    public ExecutionType formProto(CompleteEntity.ExecutionType executionType) {
        return getExecutionType(executionType);
    }
}

